#include "questiondialog.h"
#include "ui_questiondialog.h"
#include "questioncard.h"
#include "debug.h"

QuestionDialog::QuestionDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::QuestionDialog)
{
    ui->setupUi(this);
    init();
}

QuestionDialog::~QuestionDialog()
{
    delete ui;
}

#include <QAbstractTextDocumentLayout>
void QuestionDialog::set(const model::QuestionClusterInfo &questionCluster)
{
    _content->setMarkdown(QString(R"(
### %1

---

#### 参考答案:

%2

---
)").arg(questionCluster.questions.front().content
    , questionCluster.standardAnswer));
#if TEST_UI
    _content->setMarkdown(_content->toMarkdown() + R"([夜泉_ly](https://blog.csdn.net/2401_86587256?type=blog))");
#endif
    setCardList(questionCluster.questions);
}

#include <QScrollBar>
void QuestionDialog::setCardList(const QList<model::QuestionInfo> &questions)
{
    if(!_scrollArea || !_container || !_container->layout()) return;
    this->hide();

    qint64 cardsSize = _cards.size(), size = questions.size();
    qDebug() << "已有" << cardsSize << "个卡片, 设置" << size << "个卡片";

    if (cardsSize < size) {
        for(int i = 0, need = size - cardsSize; i < need; ++i) {
            auto card = new QuestionCard(_container);
            _cards.push_back(card);
            _container->layout()->addWidget(card);
        }
    } else if (cardsSize > size) {
        for (int i = size; i < cardsSize; ++i) {
            _container->layout()->removeWidget(_cards[i]);
            _cards[i]->deleteLater();
        }
        _cards.resize(size);
    }

    for(int i = 0; i < size; ++i) {
        auto& question = questions[i];
        _cards[i]->set(question);
        QObject::disconnect(_cards[i], &QuestionCard::clicked, nullptr, nullptr);
        connect(_cards[i], &QuestionCard::clicked, [this, question]{
            this->turnToInterview(question);
        });
    }

    _scrollArea->verticalScrollBar()->setValue(0);
    this->show();
}

void QuestionDialog::turnToInterview(const model::QuestionInfo &question)
{
    qDebug() << question.content << " 被点击";
}

void QuestionDialog::init()
{
    initPanel();
    initBody();
    initStyleSheet();
}

void QuestionDialog::initStyleSheet()
{
    ui->panel->setStyleSheet(R"(background-color: rgb(247, 249, 252); border-radius: 12px; color: #333333;)");
    ui->head->setStyleSheet(R"(
    #head {
        background-color: transparent;
    })");
    ui->btn_close->setStyleSheet(R"(
    QPushButton {
        background-repeat: no-repeat;
        background-position: center;
        background-image: url(:/ui/btn_close_50.png);
        border: none;
        background-color: transparent;
    }

    QPushButton:hover,
    QPushButton:pressed {
        background-image: url(:/ui/btn_close_100.png);
    })");
}

#include <QGraphicsDropShadowEffect>
void QuestionDialog::initPanel()
{
    ui->panel->setFixedSize(1200, 900);

    // 顶层(MainWidget)：无边框 + 透明，仅做容器
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    // 给内容面板(panel)加阴影
    auto *effect = new QGraphicsDropShadowEffect(ui->panel);
    effect->setColor(Qt::black);
    effect->setOffset(0, 0); // 0偏移
    effect->setBlurRadius(6); // 模糊半径
    ui->panel->setGraphicsEffect(effect);

    ui->panel->layout()->setSpacing(0);
    ui->panel->layout()->setContentsMargins(0, 10, 0, 10);

    ui->head->layout()->setSpacing(20);
    ui->head->layout()->setContentsMargins (20, 0, 20, 0);
    ui->head->setFixedHeight(60);

    ui->btn_close->setFixedSize(45, 45);
}

#include <QScrollArea>
#include <QTextBrowser>
void QuestionDialog::initBody()
{
    // 1) 新建一个主布局，设置给 ui->body
    QVBoxLayout *mainLayout = new QVBoxLayout(ui->body);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // 2) 新建 QScrollArea 及其内部容器
    _scrollArea = new QScrollArea(ui->body);

    _container = new QWidget;
    _container->setStyleSheet(R"( background-color: transparent;)");

    QVBoxLayout* containerLayout = new QVBoxLayout(_container);
    containerLayout->setSpacing(10);
    containerLayout->setContentsMargins(20, 0, 20, 0);
    containerLayout->setAlignment(Qt::AlignTop); // 布局内所有子控件顶对齐

    // 3) 设置 scrollArea
    _scrollArea->setWidget(_container);
    _scrollArea->setWidgetResizable(true);
    _scrollArea->setStyleSheet(R"( QScrollArea { border: none; background-color: transparent; } QScrollArea > QWidget { background-color: transparent; } QScrollBar:vertical { background-color: transparent; /* 透明背景 */ width: 9px; border: none; } QScrollBar::handle:vertical { background-color: #dcdfe4; border-radius: 4px; min-height: 20px; /* 最小高度 */ } QScrollBar::handle:vertical:hover { background-color: #bec3ca; } QScrollBar::handle:vertical:pressed { background-color: #a8adb5; /* 新增:按下状态 */ } /* 关键:定义滑块上下的空白区域 */ QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { background: transparent; } QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical { height: 0px; /* 隐藏箭头 */ background: transparent; } QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical { background: none; } )");

    // 4) 将 scrollArea 加到主布局里
    mainLayout->addWidget(_scrollArea);

    _content = new QTextBrowser(_container);
    _content->setOpenExternalLinks(true);
    _content->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    _content->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    _content->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _content->setFixedHeight(700);

    _container->layout()->addWidget(_content);

    auto hint = new QPushButton("相关问题: ", _container);
    hint->setStyleSheet("text-align: left; padding-left: 20px; background-color:transparent;");
    hint->setFont(QFont("微软雅黑", 10));
    _container->layout()->addWidget(hint);
}

#include <QMouseEvent>
void QuestionDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        _dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
        _isDraging = true;
        event->accept();
    }
}

void QuestionDialog::mouseMoveEvent(QMouseEvent *event)
{
    if ((event->buttons() & Qt::LeftButton) && _isDraging) {
        move(event->globalPosition().toPoint() - _dragPosition);
        event->accept();
    }
}

void QuestionDialog::mouseReleaseEvent(QMouseEvent *event)
{
    (void)event;
    _isDraging = false;
}

void QuestionDialog::on_btn_close_clicked()
{
    QuestionDialog::reject();
}

